package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.qf.feign.SearchFeigh;
import com.qf.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private EurekaClient eurekaClient;

    //restTemplate是Spring封装的java服务端发送http协议的远程调用工具
    @Autowired
    private RestTemplate restTemplate;

    @Autowired

    private SearchFeigh searchFeigh;
    //测试远程调用底层原理
    @RequestMapping("Customer")
    public String customer(){
        //1.通过eurekaClient获取Search服务的信息
        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);

        //2.从搜索服务对象中搜索微服务的IP和端口号
        String url = info.getHomePageUrl();
        System.out.println("---------"+url);
        //3.通过restTemplate对象发送http协议远程调用
        String s = restTemplate.getForObject( "http://QF-SEARCH/search/test", String.class);


        //4.返回结果
        System.out.println("--------"+s);
        return s;

    }
    //测试feign远程调用接口
    @RequestMapping("/testFeign")
    public String testFeign(){
        //调用feign接口方法
        String str = searchFeigh.test1();
        System.out.println("--------"+str);
        return str;
    }
    @RequestMapping("/findUser")
    public User testUser(){
        User user = searchFeigh.findUserById(1);
        return user;
    }


}
